package com.xmu.edu.tisp.contract.plantapplication;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.xmu.edu.tisp.R;

public class PlantDetailAdapter extends BaseAdapter{
	private Context context;
	private List<HashMap<String, String>> list;
    public PlantDetailAdapter(Context context,List<HashMap<String, String>> list){
    	super();
    	this.context=context;
		this.list = list;
    }
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final int index=position;
		View view=convertView;
		if(view==null){
			LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		    view = inflater.inflate(R.layout.plantapplication_detail_item, null);
		 
		}
		HashMap<String, String> map = list.get(position);
		TextView field_cd=(TextView) view.findViewById(R.id.field_cd);
		field_cd.setText( map.get("FIELD_BLOCK_CODE"));
		
		TextView field_name=(TextView) view.findViewById(R.id.field_name);
		field_name.setText( "("+map.get("FIELD_MANAGER")+")");
		
		TextView leaf_variety = (TextView) view.findViewById(R.id.leaf_variety);
		leaf_variety.setText( map.get("LEAF_VARIETY_NAME"));
		
		ImageButton delete = (ImageButton) view.findViewById(R.id.field_delete);
		delete.setTag(position);
		delete.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.putExtra("DELETE_ITEM", list.get(index).get("index"));
				intent.putExtra("FARMER_CD", list.get(index).get("FARMER_CD"));
				intent.putExtra("FARMER_NAME", list.get(index).get("FARMER_NAME"));
				intent.putExtra("XJSZ_ZYMD_ID", list.get(index).get("XJSZ_ZYMD_ID"));
				intent.setClass(context, Delete_Confirm_Dialog.class);
				context.startActivity(intent);
//				list.remove(index);
//				notifyDataSetChanged();
			}
			
		});
		
		
		return view;
	}
	public List<HashMap<String, String>> getList(){
		return list;
		
	}
}
